package db

import "go_code/gin-blog/model"

////获取全部评论
//func GetCommentList(articleId int64) ([]*model.Comment, error) {
//	sqlStr := `select id,content,username,create_time,article_id,status
//				from comment
//				where article_id = ?
//				and status = 1
//				order by create_time desc
//				`
//	commentList := []*model.Comment{}
//	if err := DB.Select(&commentList, sqlStr, articleId); err != nil {
//		return nil, err
//	}
//	return commentList, nil
//}
//1.获取全部评论
func GetCommentList(articleId int64) ([]*model.Comment, error) {
	commentList := []*model.Comment{}
	if err := DB.Debug().Table("comment").Where("article_id=? and status = 1", articleId).Order("create_time desc").Find(&commentList).Error; err != nil {
		return nil, err
	}
	return commentList, nil
}

////提交评论
//func InsertComment(comment *model.Comment) bool {
//	sqlStr := `insert into comment(content,username,article_id) values(?,?,?)`
//	_, err := DB.Exec(sqlStr, comment.Content, comment.UserName, comment.Article_id)
//	if err != nil {
//		return false
//	}
//	return true
//}
func InsertComment(comment *model.Comment) bool {
	if err := DB.Table("comment").Create(comment).Error; err != nil {
		return false
	}
	return true
}
